package chatBot.service.impl;

import chatBot.service.TokenConsumptionService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class TokenConsumptionServiceImpl implements TokenConsumptionService {

    @Value("${token.base-consumption}")
    private int baseConsumption;

    @Value("${token.prompt-token-rate}")
    private int promptTokenRate;

    @Value("${token.context-token-rate}")
    private int contextTokenRate;

    @Value("${token.response-token-rate}")
    private int responseTokenRate;

    @Value("${token.max-tokens}")
    private int maxTokens;

    @Override
    public int calculateTotalTokenConsumption(int promptTokens, int contextTokens, int responseTokens) {
        int totalConsumption = baseConsumption;

        totalConsumption += Math.ceil((double) promptTokens / promptTokenRate);
        totalConsumption += Math.ceil((double) contextTokens / contextTokenRate);
        totalConsumption += Math.ceil((double) responseTokens / responseTokenRate);

        return totalConsumption;
    }

    @Override
    public int calculateTokenCount(String text) {
        return (int) Math.ceil((double) text.length() / 4); // 假设每4个字符是1个token，实际情况可能不同
    }

    @Override
    public int calculateMaxResponseTokens(int promptTokens, int contextTokens) {
        return maxTokens - promptTokens - contextTokens;
    }
}
